@echo off

rem ---------------
rem Local variables
rem ---------------

rem @set BCB_VER=C5
@set BCB_VER=%1

@set MAKE=%2
@set bpr2mak=%3
@set BPL=%4
@set LIB=%5

@set inst_log=%6

@if not defined MAKE goto ParamsError
@if not defined bpr2mak goto ParamsError
@if not defined BPL goto ParamsError
@if not defined LIB goto ParamsError

@if not exist %BPL% mkdir %BPL%
@if not exist %LIB% mkdir %LIB%

@if exist %LIB%\QBuilder??_%BCB_VER%.lib del %LIB%\QBuilder??_%BCB_VER%.lib /Q
@if exist %LIB%\QBuilder??_%BCB_VER%.bpi del %LIB%\QBuilder??_%BCB_VER%.bpi /Q
@if exist %BPL%\QBuilder??_%BCB_VER%.bpl del %BPL%\QBuilder??_%BCB_VER%.bpl /Q

rem ------------------
rem Deleting old files
rem ------------------

@if exist error.log del error.log

@if exist *.~?? del *.~?? > nul
@if exist *.bpl del *.bpl > nul
@if exist *.dcp del *.dcp > nul
@if exist *.dcu del *.dcu > nul
@if exist *.obj del *.obj > nul
@if exist *.hpp del *.hpp > nul
@if exist *.lib del *.lib > nul
@if exist *.bpi del *.bpi > nul
@if exist *.tds del *.tds > nul

rem -------------------
rem Creating make files
rem -------------------

bpr2mak -q -t%BCB_VER%.bmk QBuilderRT_%BCB_VER%.bpk > nul
bpr2mak -q -t%BCB_VER%.bmk QBuilderDT_%BCB_VER%.bpk > nul

rem --------------------------
rem Compiling run-time package
rem --------------------------

@if exist QBuilderRT_%BCB_VER%.mak %MAKE% -fQBuilderRT_%BCB_VER%.mak -B >> error.log
@if errorlevel=1 goto RTCommonError
@if exist error.log del error.log > nul
@echo QBuilderRT_%BCB_VER%.bpk was compiled successfully! >>error.log

rem -----------------------------
rem Compiling design-time package
rem -----------------------------

@if exist QBuilderDT_%BCB_VER%.mak %MAKE% -fQBuilderDT_%BCB_VER%.mak -B >> error.log
@if errorlevel=1 goto DTCommonError
@if exist error.log del error.log > nul
@echo QBuilderDT_%BCB_VER%.bpk was compiled successfully! >>error.log

rem --------------------
rem Copying result files
rem --------------------

@copy *%BCB_VER%.bpl %BPL% /Y > nul
@copy *%BCB_VER%.bpi %LIB% /Y > nul
@copy *%BCB_VER%.lib %LIB% /Y > nul

rem --------------------
rem Save compiled files
rem --------------------

@if not exist ..\%BCB_VER% mkdir ..\%BCB_VER%

@if exist *.dcu copy *.dcu ..\%BCB_VER% > nul
@if exist *.obj copy *.obj ..\%BCB_VER% > nul
@if exist *.hpp copy *.hpp ..\%BCB_VER% > nul
@if exist *.dfm copy *.dfm ..\%BCB_VER% > nul
@if exist *.res copy *.res ..\%BCB_VER% > nul

rem -----------------------
rem Deleting needless files
rem -----------------------

@if exist *.mak del *.mak > nul
@if exist *.dcu del *.dcu > nul
@if exist *.obj del *.obj > nul
@if exist *.hpp del *.hpp > nul
@if exist *.lib del *.lib > nul
@if exist *.bpi del *.bpi > nul
@if exist *.bpl del *.bpl > nul
@if exist *.tds del *.tds > nul

goto End

:RTCommonError
@echo QBuilderRT_%BCB_VER%.bpk was compiled with errors! >>error.log
@echo QBuilderRT_%BCB_VER%.bpk >>%inst_log%
rem @"error.log"
rem @pause
@exit 10

:DTCommonError
@echo QBuilderDT_%BCB_VER%.bpk was compiled with errors! >>error.log
@echo QBuilderDT_%BCB_VER%.bpk >>%inst_log%
rem @"error.log"
rem @pause
@exit 20

:ParamsError
@echo Empty params!
rem @pause
@exit 30

:End